/************************************************************
	File:		BitmapFont.cpp
	Author:		Roberto Crespo, Keisuke Miyazaki
	Purpose:	Handles Game Font
************************************************************/
#include "BitmapFont.h"
#include "..\Animation System\Animation.h"

#include "../../SGD Wrappers/CSGD_TextureManager.h"
#include "../../SGD Wrappers/SGD_String.h"

CBitmapFont::CBitmapFont()
{
	m_nImageID		= -1;

	m_nCharWidth	= 0;
	m_nCharHeight	= 0;
	m_nNumCols		= 0;
	m_nNumRows		= 0;

	m_cFirstChar	= 0;
}

void CBitmapFont::Initialize()
{
// 	switch ( m_fFontType )
// 	{
// 	case Cursive:
// 		{
			m_nImageID		= CSGD_TextureManager::GetInstance()->LoadTexture(
				_T("resource/graphics/Font/Font_Arial.png"), D3DCOLOR_XRGB( 63, 63,63 ) );

			m_nCharWidth	= 64;
			m_nCharHeight	= 43;
			m_nNumCols		= 16;
			m_nNumRows		= 8;

			m_cFirstChar	= _T(' ');
// 		}
// 		break;
// 	case Arial:
// 		{
// 
// 		}
// 		break;
// 	}
}
void CBitmapFont::Terminate()
{
	CSGD_TextureManager::GetInstance()->UnloadTexture( m_nImageID );
	m_nImageID = -1;
}

void CBitmapFont::Draw(const TCHAR* strOutput, int nX, int nY, float fScale, DWORD dwColor ) const
{
	int nColStart = nX;

	for( int i = 0; strOutput[ i ]; i++ )
	{
		TCHAR ch = strOutput[ i ];

		if( ch == _T(' ') )
		{
			nX += (int)(m_nCharWidth * fScale);
			continue;
		}
		else if( ch == _T('\n') )
		{
			nY += (int)(m_nCharHeight * fScale);
			nX =  nColStart;
			continue;
		}
		else if( ch == _T('\t') )
		{
			int diff = (int)((nX - nColStart) / (m_nCharWidth * fScale));

			int tab  = 4 - (diff % 4);

			nX += (int)(tab * m_nCharWidth * fScale);
			continue;
		}

		int id = ch - m_cFirstChar;
		RECT rCell = CellAlgorithm( id );

		CSGD_TextureManager::GetInstance()->Draw(
			m_nImageID, nX, nY, 
			fScale, fScale * 0.9f, &rCell,
			0.0f, 0.0f, 0.0f, dwColor );

		nX += (int)(m_nCharWidth * fScale);
	}
}

RECT CBitmapFont::CellAlgorithm( int _id ) const
{
	RECT rChar = { };

	rChar.left		= (_id % m_nNumCols) * m_nCharWidth;
	rChar.top		= (_id / m_nNumCols) * m_nCharHeight;

	rChar.right		= rChar.left + m_nCharWidth;
	rChar.bottom	= rChar.top  + m_nCharHeight;

	return rChar;
}

void CBitmapFont::SetFont( FONT _Font )
{
	m_fFontType = _Font;
}
